//
//  ViewController.m
//  26qq音乐界面masonry
//
//  Created by dllo on 16/8/18.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#import "Masonry.h"
#import <AVFoundation/AVFoundation.h>
#import "UIView+Frame.h"
#import <AVKit/AVKit.h>
@interface ViewController ()
@property (nonatomic,strong) NSArray *imageArr;
/* 背景图片 */
@property (nonatomic, strong) UIImageView *backgroundImageView;
/* 模糊效果 */
@property (nonatomic, strong) UIVisualEffectView *effectView;
/* 黑色view */
@property (nonatomic, strong) UIView *blackView;
/* 左上角closeButton */
@property (nonatomic, strong) UIButton *closeButton;
/* 右上角moreButton */
@property (nonatomic, strong) UIButton *moreButton;
/* 中间圆形图片*/
@property (nonatomic, strong) UIImageView *middleImageView;
/* 滑条 */
@property (nonatomic, strong) UISlider *slider;
/* 上一首歌button*/
@property (nonatomic, strong) UIButton *preButton;
/* 下一首歌button*/
@property (nonatomic, strong) UIButton *nextButton;
/* 播放暂停button*/
@property (nonatomic, strong) UIButton *playAndPauseButton;
/* airPlaybutton*/
@property (nonatomic, strong) UIButton *airPlayButton;
/* favourtButton*/
@property (nonatomic, strong) UIButton *favourtButton;
@property (nonatomic, strong) UIButton *repeatButton;
@property (nonatomic, strong) UIButton *downLoadButton;
@property (nonatomic, strong) UIButton *shareButton;
@property (nonatomic, strong) UIButton *otherButton;

//声明播放音频的控件属性
@property (nonatomic,strong) AVPlayer *player;
//播放的总时长
@property (nonatomic,assign) CGFloat sumPlayOperation;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    1.完善今天最后的练习, 添加下一曲和上一曲功能
//    2.调研AVPlayer, 使QQ音乐播放页面拥有播放功能(播放、暂停、进度条滑动), 音乐播放链接自寻
//    3.一篇博客
//    4.周报
    
//    设置播放的url
    NSString *playString = @"http://static.tripbe.com/videofiles/20121214/9533522808.f4v.mp4";
    NSURL *url = [NSURL URLWithString:playString];
//    设置播放的项目
    AVPlayerItem *item = [[AVPlayerItem alloc] initWithURL:url];
//    初始化player对象
    _player = [[AVPlayer alloc] initWithPlayerItem:item];
//    设置播放页面
    AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:_player];
//    设置播放页面大小
    layer.frame = CGRectMake(0, 0, self.view.bounds.size.width, 300);
    layer.backgroundColor = [UIColor cyanColor].CGColor;
//    设置播放窗口和当前视图之间的比例显示内容
    layer.videoGravity = AVLayerVideoGravityResizeAspect;
//    添加播放视图到self.view
    [self.view.layer addSublayer:layer];
    
    _slider.value = 0;
//    设置播放的默认音量值
    _player.volume = 1.0f;
    
    // 背景图
//    self.backgroundImageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    
    
    
    
    
    
    
    
    _backgroundImageView = [UIImageView new];
    [self.view addSubview:_backgroundImageView];
    _backgroundImageView.image = [UIImage imageNamed:@"QQ音乐"];
    [_backgroundImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.right.mas_equalTo(0);
    }];
    
    
//    _backgroundImageView.image = [UIImage imageNamed:@"QQ音乐"];
//    [self.view addSubview:_backgroundImageView];
//     模糊效果
    UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    self.effectView = [[UIVisualEffectView alloc]initWithEffect:effect];
    [self.view addSubview:_effectView];
//    _effectView.frame = CGRectMake(0, 0, 375, 667);
    [_effectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.mas_equalTo(0);
    }];
    _effectView.alpha = 0.7;
    _effectView.tag = 2000;
//
//    //黑色背景
//    _blackView = [[UIView alloc] initWithFrame:_backgroundImageView.frame];
    _blackView = [UIView new];
    [_backgroundImageView addSubview:_blackView];
    _blackView.backgroundColor = [UIColor blackColor];
    _blackView.alpha = 0.8;
    [_blackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.left.bottom.mas_equalTo(0);
    }];
//    _blackView.backgroundColor = [UIColor blackColor];
//    _blackView.alpha = 0.8;
//    [_backgroundImageView addSubview:_blackView];
//
//    //左上角imageViwe
    //    self.arrowImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 20, 42, 42)];
    //    _arrowImageView.image = [UIImage imageNamed:@"miniplayer_btn_playlist_close_b"];
    //    [self.view addSubview:_arrowImageView];
    self.closeButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:_closeButton];
//    _closeButton.frame = CGRectMake(10, 20, 40, 40);
    [_closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.left.mas_equalTo(20);
        make.width.mas_equalTo(40);
        make.height.mas_equalTo(40);
    }];
    [_closeButton setImage:[[UIImage imageNamed:@"miniplayer_btn_playlist_close_b"] imageWithRenderingMode:1] forState:UIControlStateNormal];
//
//    //右上角moreButton
    self.moreButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:_moreButton];
//    _moreButton.frame = CGRectMake(325, 20, 40, 40);
    [_moreButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(325);
        make.top.mas_equalTo(20);
        make.width.mas_equalTo(40);
        make.height.mas_equalTo(40);
    }];
    [_moreButton setImage:[[UIImage imageNamed:@"allMusic_more"]imageWithRenderingMode:1] forState:UIControlStateNormal];
//
//    //中间圆形图片
//    self.middleImageView = [[UIImageView alloc] initWithFrame:CGRectMake(17, 130, 340, 340)];
    _middleImageView = [UIImageView new];
    [self.view addSubview:_middleImageView];
    [_middleImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(17);
        make.top.mas_equalTo(130);
        make.width.height.mas_equalTo(340);
    }];
    _middleImageView.layer.cornerRadius = 170;
    _middleImageView.image = [UIImage imageNamed:@"QQ音乐"];
//
//    self.slider = [[UISlider alloc] initWithFrame:CGRectMake(45, 530, 285, 10)];
    //    // 滑条
    _slider = [UISlider new];
    [self.view addSubview:_slider];
[_slider mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.mas_equalTo(45);
    make.top.mas_equalTo(530);
    make.width.mas_equalTo(285);
    make.height.mas_equalTo(10);
}];
    _slider.minimumValue = 00.00;
    _slider.maximumValue = 04.35;
    [_slider setMinimumTrackImage:[UIImage imageNamed:@"player_slider_playback_left"] forState:UIControlStateNormal];
    [_slider setMaximumTrackImage:[UIImage imageNamed:@"player_slider_playback_right"] forState:UIControlStateNormal];
    [_slider setThumbImage:[UIImage imageNamed:@"player_slider_playback_thumb"] forState:UIControlStateNormal];
//    [_slider addTarget:self action:@selector(didClickedSlider:) forControlEvents:UIControlEventValueChanged];

    [_slider addTarget:self action:@selector(changeProgress) forControlEvents:UIControlEventTouchUpInside];
//
//    // 上一首歌button
    self.preButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:_preButton];
//    _preButton.frame = CGRectMake(75, 545, 64, 64);
    [_preButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(75);
        make.top.mas_equalTo(545);
        make.width.height.mas_equalTo(64);
    }];
    [_preButton setBackgroundImage:[UIImage imageNamed:@"fllplayer_btn_pre_normal"] forState:UIControlStateNormal];
    [_preButton addTarget:self action:@selector(clickedPreButton) forControlEvents:UIControlEventTouchUpInside];
//
//    // 播放暂停button
    self.playAndPauseButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:_playAndPauseButton];
//    _playAndPauseButton.frame = CGRectMake(160, 545, 64, 64);
    [_playAndPauseButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(160);
        make.top.mas_equalTo(545);
        make.width.height.mas_equalTo(64);
    }];
    [_playAndPauseButton setBackgroundImage:[UIImage imageNamed:@"player_btn_play_normal"] forState:UIControlStateNormal];
    [_playAndPauseButton addTarget:self action:@selector(didPlayAndPauseButton:) forControlEvents:UIControlEventTouchUpInside];
//
//    // 下一首歌button
    self.nextButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:_nextButton];
//    _nextButton.frame = CGRectMake(245, 545, 64, 64);
    [_nextButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(245);
        make.top.mas_equalTo(545);
        make.width.height.mas_equalTo(64);
    }];
    [_nextButton setBackgroundImage:[UIImage imageNamed:@"player_btn_next_normal"] forState:UIControlStateNormal];
    [_nextButton addTarget:self action:@selector(clickedNextButton) forControlEvents:UIControlEventTouchUpInside];
//
//    // airPlayButton
    self.airPlayButton = [UIButton buttonWithType:UIButtonTypeSystem];

//    _airPlayButton.frame = CGRectMake(328, 567, 32, 32);
    [self.view addSubview:_airPlayButton];
    [_airPlayButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(328);
        make.top.mas_equalTo(567);
        make.width.height.mas_equalTo(32);
    }];
    [_airPlayButton setBackgroundImage:[UIImage imageNamed:@"player_btn_airplay_normal"] forState:UIControlStateNormal];
//
//    UILabel *leftTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 520, 35, 20)];
    UILabel *leftTimeLabel = [UILabel new];
    [self.view addSubview:leftTimeLabel];
    [leftTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(5);
        make.top.mas_equalTo(520);
        make.width.mas_equalTo(35);
        make.height.mas_equalTo(20);
    }];
    leftTimeLabel.tag = 1000;
    leftTimeLabel.text = @"00.00";
    leftTimeLabel.font = [UIFont systemFontOfSize:12];
    leftTimeLabel.textColor = [UIColor whiteColor];
//
//    UILabel *rightTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(335, 520, 35, 20)];
    UILabel *rightTimeLabel = [UILabel new];
    [self.view addSubview:rightTimeLabel];
    [rightTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(335);
        make.top.mas_equalTo(520);
        make.width.mas_equalTo(35);
        make.height.mas_equalTo(20);
    }];
    rightTimeLabel.textColor = [UIColor whiteColor];
    rightTimeLabel.text = @"04.35";
    rightTimeLabel.font = [UIFont systemFontOfSize:12];
//
    self.favourtButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:_favourtButton];
//    _favourtButton.frame = CGRectMake(25, 610, 50, 50);
    [_favourtButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(25);
        make.top.mas_equalTo(610);
        make.width.height.mas_equalTo(50);
    }];
    [_favourtButton setBackgroundImage:[UIImage imageNamed:@"player_btn_favorited_normal"] forState:UIControlStateNormal];
//
    self.repeatButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:_repeatButton];
//    _repeatButton.frame = CGRectMake(95, 610, 50, 50);
    [_repeatButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(95);
        make.top.mas_equalTo(610);
        make.width.height.mas_equalTo(50);
    }];
    [_repeatButton setBackgroundImage:[UIImage imageNamed:@"player_btn_repeat_disable"] forState:UIControlStateNormal];
//
    self.downLoadButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:_downLoadButton];
//    _downLoadButton.frame = CGRectMake(165, 610, 50, 50);
    [_downLoadButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(165);
        make.top.mas_equalTo(610);
        make.width.height.mas_equalTo(50);
    }];
    [_downLoadButton setBackgroundImage:[UIImage imageNamed:@"actionIconDownloaded_h_b_1"] forState:UIControlStateNormal];
    [_downLoadButton setBackgroundImage:[UIImage imageNamed:@"actionIconDownloaded_2"] forState:UIControlStateNormal];
//
    self.shareButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:_shareButton];
//    _shareButton.frame = CGRectMake(235, 610, 50, 50);
    [_shareButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(235);
        make.top.mas_equalTo(610);
        make.width.height.mas_equalTo(50);
    }];
    [_shareButton setBackgroundImage:[UIImage imageNamed:@"player_btn_share_disable"] forState:UIControlStateNormal];
//
    self.otherButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:_otherButton];
//    _otherButton.frame = CGRectMake(305, 610, 50, 50);
    [_otherButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(610);
        make.left.mas_equalTo(305);
        make.height.width.mas_equalTo(50);
    }];
    [_otherButton setBackgroundImage:[UIImage imageNamed:@"miniplayer_btn_playlist_disable"] forState:UIControlStateNormal];
//
//    UILabel *titileLabel = [[UILabel alloc] initWithFrame:CGRectMake(150, 50, 75, 25)];
    UILabel *titileLabel = [UILabel new];
    [self.view addSubview:titileLabel];
    [titileLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(150);
        make.top.mas_equalTo(50);
        make.width.mas_equalTo(75);
        make.height.mas_equalTo(25);
    }];
    titileLabel.text = @"Sugar";
    //    titileLabel.backgroundColor = [UIColor redColor];
    titileLabel.font = [UIFont systemFontOfSize:22];
    titileLabel.textAlignment = 1;
    titileLabel.textColor = [UIColor whiteColor];
//
//    UIPageControl *page = [[UIPageControl alloc] initWithFrame:CGRectMake(150, 480, 75, 50)];
    UIPageControl *page = [UIPageControl new];
    [self.view addSubview:page];
    [page mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(150);
        make.top.mas_equalTo(480);
        make.width.mas_equalTo(75);
        make.height.mas_equalTo(50);
    }];
    page.numberOfPages = 3;
    page.currentPage = 1;
}

- (void)clickedPreButton
{
//    _middleImageView.image = [UIImage imageNamed:@"0"];
    
}

- (void)clickedNextButton
{
//    _middleImageView.image = [UIImage imageNamed:@"1"];
}



- (void)didPlayAndPauseButton:(UIButton *)button
{
    
    if (_isClicking == YES) {
        [button setBackgroundImage:[UIImage imageNamed:@"player_btn_play_normal"] forState:UIControlStateNormal];
        _isClicking = NO;
        [_player pause];
    } else {
        
        [button setBackgroundImage:[UIImage imageNamed:@"player_btn_pause_normal"] forState:UIControlStateNormal];
        _isClicking = YES;
        [_player play];
    }
    
    
    
    
}


//改变进度
- (void)changeProgress
{
    self.sumPlayOperation = self.player.currentItem.duration.value/self.player.currentItem.duration.timescale;
    //CMTimeMake(a,b) a表示当前时间，b表示每秒钟多少帧
    [_player seekToTime:CMTimeMakeWithSeconds(_slider.value * _sumPlayOperation, _player.currentItem.duration.timescale) completionHandler:^(BOOL finished) {
        [_player play];
    }];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
